package com.onec.service.customer.service.impl;

import cn.hutool.core.date.DateUtil;
import com.onec.service.api.enums.ProductEnum;
import com.onec.service.api.enums.StatusEnum;
import com.onec.service.customer.dao.merchant.MerchantAccountRepository;
import com.onec.service.customer.entity.merchant.MerchantAccount;
import com.onec.service.customer.entity.merchant.MerchantInfo;
import com.onec.service.customer.service.MerchantAccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Optional;

/**
 * 商户账户信息
 *
 * @author onec
 * @see
 * @since 20210108
 */
@Service
public class MerchantAccountServiceImpl implements MerchantAccountService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MerchantAccountServiceImpl.class);

    @Autowired
    MerchantAccountRepository accountRepository;

    @Override
    public MerchantAccount findByMerchantId(Long merchantId) {
        return accountRepository.findByMerchantId(merchantId);
    }

    @Override
    public MerchantAccount findByMerchantIdBusinessNo(Long merchantId, String businessNo) {
        return accountRepository.findByMerchantIdAndBusinessNo(merchantId, businessNo);
    }


    @Override
    public MerchantAccount createMerchantAccount(MerchantInfo merchantInfo) {
        MerchantAccount merchantAccount = this.findByMerchantId(merchantInfo.getId());
        if (Optional.ofNullable(merchantAccount).isPresent()) {
            LOGGER.info("当前商户号：{}，商户注册，该账户已存在：", merchantInfo.getCode());
            return merchantAccount;
        }
        merchantAccount = new MerchantAccount();
        merchantAccount.setPhone(merchantInfo.getPhone());
        merchantAccount.setContacts(merchantInfo.getName());
        merchantAccount.setProduct(ProductEnum.DEFAULT);
        merchantAccount.setBusinessNo("00000");
        return this.createMerchantAccount(merchantAccount);
    }

    /**
     * 商户业务账户，配置细粒度较高：
     *
     * @param merchantInfo 商户信息
     * @param businessNo   业务信息
     * @return
     */
    @Override
    public MerchantAccount createMerchantAccountBusinessNo(MerchantInfo merchantInfo, String businessNo) {
        MerchantAccount merchantAccount = this.findByMerchantIdBusinessNo(merchantInfo.getId(), businessNo);
        if (Optional.ofNullable(merchantAccount).isPresent()) {
            LOGGER.info("当前商户号：{}，业务名称：{}，该账户已存在：", merchantInfo.getCode(), businessNo);
            return merchantAccount;
        }
        merchantAccount = new MerchantAccount();
        merchantAccount.setPhone(merchantInfo.getPhone());
        merchantAccount.setBusinessNo(businessNo);
        merchantAccount.setProduct(ProductEnum.DEFAULT);
        merchantAccount.setMerchantId(merchantInfo.getId());
        return this.createMerchantAccount(merchantAccount);
    }

    /**
     * 创建账户：
     */
    private MerchantAccount createMerchantAccount(MerchantAccount merchantAccount) {
        merchantAccount.setFreezeAmount(new BigDecimal("0"));
        merchantAccount.setTotalAmount(new BigDecimal("0"));
        merchantAccount.setUseAmount(new BigDecimal("0"));
        merchantAccount.setVersion(1);
        merchantAccount.setRemarks("商户账户");
        //merchantAccount.setContacts(merchantInfo.getContacts());
        merchantAccount.setCreator("商户入件");
        merchantAccount.setStatus(StatusEnum.SUCCESS);
        merchantAccount.setCreateTime(DateUtil.date());
        merchantAccount.setUpdateTime(DateUtil.date());

        LOGGER.info("商户编号：{}，账户配置完成：初始余额：{}", merchantAccount.getMerchantId(), merchantAccount.getTotalAmount());
        accountRepository.save(merchantAccount);
        return merchantAccount;
    }
}
